package com.zhong.wechat.handler;

import com.zhong.wechat.enums.WeChatMessageTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信消息工厂
 */
@Component
public class WeChatMsgFactory implements InitializingBean {
    @Resource
    private List<WeChatMegHandler> weChatMegHandlerList;

    private final Map<WeChatMessageTypeEnum,WeChatMegHandler> handlerMap = new HashMap<>();

    public WeChatMegHandler getHandlerByMsgType(String msgType){
        WeChatMessageTypeEnum msgTypeEnum = WeChatMessageTypeEnum.getByMsgType(msgType);
        return handlerMap.get(msgTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WeChatMegHandler weChatMegHandler : weChatMegHandlerList) {
            handlerMap.put(weChatMegHandler.getMsgType(),weChatMegHandler);
        }
    }
}
